package by.ins.webconsole.jsf.actions;


import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.context.ExternalContext;

import by.yativem.commons.beans.Beans;

public class ActionSupport {	
	public static final String RESULT_SUCCESS ="success";
	public static final String RESULT_ERROR ="error";
	
	protected List<String> actionErrorMssgs;
	//protected List<String> getAction
	public Boolean hasErrors(){
		return  (actionErrorMssgs != null && !actionErrorMssgs.isEmpty() ); 
	}
	
	
	protected void addError(String msg){
		if (actionErrorMssgs == null){
			actionErrorMssgs = new ArrayList<String>(2);
		}
		actionErrorMssgs.add(msg);
	}
	
	public void bindContextParam(){
		ExternalContext externalContext =  javax.faces.context.FacesContext.getCurrentInstance().getExternalContext();
		Iterator<String> it =  externalContext.getRequestParameterNames();
		
		if(it== null) return;
		String pn="";
		while( it.hasNext()  ){
			pn=  it.next();
		 	Method m=  Beans.getSetMethod( pn, this.getClass());
		 	if(m != null){
		 		try {
					m.invoke(this, externalContext.getRequestParameterMap().get(pn) );
				} catch (Exception e) {
				//	log
				}
		 	}			
		}		
		
		
		
	}

}
